<?php

/**
 * Api接口
 * Yii用法示例：
 *      Yii::import('ext.SsoClient.*');
 *      $api = new Api();
 * File Name：Api.php
 * File Encoding：UTF-8
 * File New Time：2014-10-21 15:21:38
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class Api {

    //配置信息
    protected $config = array(
        'client_id' => 0, //站点id
        'client_key' => '', //站点密钥
        'key_validity' => 3000, //密文有效期，单位秒
    );

    public function __construct($config = array()) {
        if (is_array($config) && !empty($config)) {
            $this->config = array_merge($config, $this->config);
        }
    }

    //执行
    public function run() {
        $code = $_GET['code'];
        parse_str(Encrypt::authcode($code, 'DECODE', $this->config['client_key']), $get);
        $post = Encrypt::authcode($_POST['code'], 'DECODE', $this->config['client_key']);
        if (!empty($post)) {
            $post = unserialize($post);
        }
        if (in_array($get['action'], array('demo', 'synlogin', 'synlogout', 'upapps', 'updatauser', 'deleteuser'))) {
            echo $this->$get['action']($get, $post);
            exit;
        } else {
            exit('0');
        }
    }

    //测试接口
    public function demo($get, $post) {
        return 1;
    }

    //同步登录
    public function synlogin($get, $post) {
        return 1;
    }

    //同步退出
    public function synlogout($get, $post) {
        return 1;
    }

    //更新用户资料
    public function updatauser($get, $post) {
        return 1;
    }

    //删除用户
    public function deleteuser($get, $post) {
        return 1;
    }

    //更新apps缓存
    public function upapps($get, $post) {
        return Fun::file('cache/apps', $post) ? 1 : 0;
    }

}
